/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.androidtest;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Database helper class for {@link SettingsProvider}. Mostly just has a bit
 * {@link #onCreate} to initialize the database.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	private final Object dbHelperLock=new Object();
	public static String DB_NAME = "sqlite.db";
	
	public static int DB_VERSION = 10;
	public static final String TABLE_IF_EXISTS_SQL = " CREATE TABLE IF NOT EXISTS ";
	private static SQLiteDatabase mWriteableDB, mReadableDB;
	protected Context context;

	public DatabaseHelper(Context context, String dbName, int dbVersion) {
		super(context, dbName, null, dbVersion);
		this.context = context;
	}

	public DatabaseHelper(Context context) {
		this(context, DB_NAME, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase sdb) {
		mWriteableDB = sdb;
		//mReadableDB=sdb;
	}

	@Override
	public SQLiteDatabase getWritableDatabase() {
		if (mWriteableDB == null || !mWriteableDB.isOpen()) {
			synchronized (dbHelperLock) {
				if (mWriteableDB == null || !mWriteableDB.isOpen()) {
					mWriteableDB = super.getWritableDatabase();
					//onCreate(mWriteableDB);
				}
			}
		}
		return mWriteableDB;
	}

	public SQLiteDatabase getReadableDatabase() {
		if (mReadableDB == null || !mReadableDB.isOpen()) {
			synchronized (dbHelperLock) {
				if (mReadableDB == null || !mReadableDB.isOpen()) {
					mReadableDB = super.getReadableDatabase();
					// createTables(mWriteableDB);
				}
			}
		}
		return mReadableDB;
	}

	public void close() {
		synchronized (dbHelperLock) {
			if (mWriteableDB != null && mWriteableDB.isOpen()) {
				mWriteableDB.close();
				mWriteableDB = null;
			}
			if (mReadableDB != null && mReadableDB.isOpen()) {
				mReadableDB.close();
				mReadableDB = null;
			}
		}
	}

	/**
	 * 数据库版本更新时的调用
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	/**
	 * 判断某张表是否存在
	 * @param tabName
	 *            表名
	 * @return
	 */
	public boolean tabbleIsExist(SQLiteDatabase db, String tableName) {
		boolean result = false;
		if (tableName == null) {
			return false;
		}
		Cursor cursor = null;
		try {
			String sql = "select count(*) as c from Sqlite_master  where type ='table' and name ='"
					+ tableName.trim() + "' ";
			cursor = db.rawQuery(sql, null);
			if (cursor.moveToNext()) {
				int count = cursor.getInt(0);
				if (count > 0) {
					result = true;
				}
			}
		} catch (Exception e) {
			Log.e("drug tabbleIsExist", "drug tabbleIsExist error", e);
		} finally {
			if (cursor != null) {
				try {
					cursor.close();
				} catch (Exception e) {
				}
			}
		}
		return result;
	}
}